package cn.haocxy.jfm.util;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.function.Consumer;

public final class Some<T> implements Iterable<T> {

	private final HashMap<T, T> map = new HashMap<>();
	
	public void add(T elem) {
		if (elem == null) {
			throw new NullPointerException();
		}
		map.put(elem, elem);
	}
	
	public int size() {
		return map.size();
	}
	
	public void remove(T elem) {
		if (elem == null) {
			throw new NullPointerException();
		}
		map.remove(elem);
	}
	
	public void clear() {
		map.clear();
	}
	
	public boolean hasElem() {
		return !map.isEmpty();
	}
	
	public boolean isEmpty() {
		return map.isEmpty();
	}
	
	public Collection<T> elems() {
		return map.values();
	}
	
	public void each(Consumer<T> action) {
		if (action == null) {
			throw new NullPointerException();
		}
		map.values().forEach(action);
	}

	@Override
	public Iterator<T> iterator() {
		return elems().iterator();
	}
}
